package com.zmn.mcc.admin.controller.dept.dpermit;

import com.zmn.common.dictionary.GlobalDict;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.cube.common.dro.measure.MeasureDRO;
import com.zmn.cube.common.dro.menu.MenuDRO;
import com.zmn.cube.dubbo.dto.DubboConsts;
import com.zmn.cube.dubbo.interfaces.measure.MeasureListRemoteService;
import com.zmn.cube.dubbo.interfaces.menu.MenuListRemoteService;
import com.zmn.mcc.business.interfaces.staff.StaffBService;
import com.zmn.mcc.common.constant.DpermitConsts;
import com.zmn.mcc.manager.utils.DpermitUtil;
import com.zmn.mcc.model.dto.dpermit.DpermitSimpleDO;
import com.zmn.mcc.model.entity.dept.McDept;
import com.zmn.mcc.model.entity.dept.McDeptDpermit;
import com.zmn.mcc.model.entity.dept.McDeptDpermitQuery;
import com.zmn.mcc.model.entity.organize.McOrganize;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author heguanghua
 * @since 2020/05/19 10:04
 */
@Controller
@RequestMapping("/dept/dpermit/")
@Slf4j
public class DeptDpermitListController extends DeptDpermitBaseController {

    @Resource
    private StaffBService staffBService;

    @DubboReference(version = DubboConsts.INTERFACE_VERSION, check = false)
    private MenuListRemoteService menuListRemoteService;

    @DubboReference(version = DubboConsts.INTERFACE_VERSION, check = false)
    private MeasureListRemoteService measureListRemoteService;


    /**
     * 组织权限树状列表
     */
    @RequestMapping("organize/tree")
    public String organizeTree(HttpServletRequest request,
                               @RequestParam(value = "deptId", defaultValue = "0") Integer deptId,
                               Model model) {

        McDept obj = NumberUtil.isNotNullOrZero(deptId) ? deptService.findByKey(deptId) : new McDept();
        McOrganize organize = organizeService.findByKey(obj.getOrgId());

        model.addAttribute("obj", obj);
        model.addAttribute("organize", organize);

        // 是否拥有"权限管理员角色"
        model.addAttribute("isAssignAdmin", staffBService.isAssignAdmin(getStaffId()));

        return "dept/dpermit/organize/tree";
    }

    @RequestMapping("organize/ajaxTree")
    @ResponseBody
    public ResponseDTO organizeAjaxTree(HttpServletRequest request, HttpServletResponse response,
                                        @RequestParam(value = "deptId", defaultValue = "0") Integer deptId) {

        ResponseDTO result = deptDpermitBService.listOrganizeDpermit(deptId, GlobalDict.STATUS_ENABLE);
        return result;
    }

    /**
     * 区域权限树状列表
     */
    @RequestMapping("area/tree")
    public String areaTree(HttpServletRequest request,
                           @RequestParam(value = "deptId", defaultValue = "0") Integer deptId,
                           Model model) {

        McDept obj = NumberUtil.isNotNullOrZero(deptId) ? deptService.findByKey(deptId) : new McDept();
        McOrganize organize = organizeService.findByKey(obj.getOrgId());

        model.addAttribute("obj", obj);
        model.addAttribute("organize", organize);

        // 是否拥有"权限管理员角色"
        model.addAttribute("isAssignAdmin", staffBService.isAssignAdmin(getStaffId()));

        return "dept/dpermit/area/tree";
    }

    @RequestMapping("area/ajaxTree")
    @ResponseBody
    public ResponseDTO areaAjaxTree(HttpServletRequest request, HttpServletResponse response,
                                    @RequestParam(value = "deptId", defaultValue = "0") Integer deptId) {

        ResponseDTO result = deptDpermitBService.listAreaDpermit(deptId, GlobalDict.STATUS_ENABLE);
        return result;
    }

    /**
     * 部门权限树状列表
     */
    @RequestMapping("dept/tree")
    public String deptTree(HttpServletRequest request,
                           @RequestParam(value = "deptId", defaultValue = "0") Integer deptId,
                           Model model) {

        McDept obj = NumberUtil.isNotNullOrZero(deptId) ? deptService.findByKey(deptId) : new McDept();
        McOrganize organize = organizeService.findByKey(obj.getOrgId());

        model.addAttribute("obj", obj);
        model.addAttribute("organize", organize);

        // 是否拥有"权限管理员角色"
        model.addAttribute("isAssignAdmin", staffBService.isAssignAdmin(getStaffId()));

        return "dept/dpermit/dept/tree";
    }

    @RequestMapping("dept/ajaxTree")
    @ResponseBody
    public ResponseDTO deptAjaxTree(HttpServletRequest request, HttpServletResponse response,
                                    @RequestParam(value = "deptId", defaultValue = "0") Integer deptId) {

        ResponseDTO result = deptDpermitBService.listDeptDpermit(deptId, GlobalDict.STATUS_ENABLE);
        return result;
    }

    /**
     * 渠道权限树状列表
     */
    @RequestMapping("channel/tree")
    public String channelTree(HttpServletRequest request,
                              @RequestParam(value = "deptId", defaultValue = "0") Integer deptId,
                              Model model) {

        McDept obj = NumberUtil.isNotNullOrZero(deptId) ? deptService.findByKey(deptId) : new McDept();
        McOrganize organize = organizeService.findByKey(obj.getOrgId());

        model.addAttribute("obj", obj);
        model.addAttribute("organize", organize);

        // 是否拥有"权限管理员角色"
        model.addAttribute("isAssignAdmin", staffBService.isAssignAdmin(getStaffId()));

        return "dept/dpermit/channel/tree";
    }

    @RequestMapping("channel/ajaxTree")
    @ResponseBody
    public ResponseDTO channelAjaxTree(HttpServletRequest request, HttpServletResponse response,
                                       @RequestParam(value = "deptId", defaultValue = "0") Integer deptId) {

        ResponseDTO result = deptDpermitBService.listChannelDpermit(deptId, GlobalDict.STATUS_ENABLE);
        return result;
    }

    @RequestMapping("newchannel/tree")
    public String newChannelTree(HttpServletRequest request,
                              @RequestParam(value = "deptId", defaultValue = "0") Integer deptId,
                              Model model) {

        McDept obj = NumberUtil.isNotNullOrZero(deptId) ? deptService.findByKey(deptId) : new McDept();
        McOrganize organize = organizeService.findByKey(obj.getOrgId());

        model.addAttribute("obj", obj);
        model.addAttribute("organize", organize);

        // 是否拥有"权限管理员角色"
        model.addAttribute("isAssignAdmin", staffBService.isAssignAdmin(getStaffId()));

        return "dept/dpermit/newchannel/tree";
    }

    @RequestMapping("newchannel/ajaxTree")
    @ResponseBody
    public ResponseDTO newChannelAjaxTree(HttpServletRequest request, HttpServletResponse response,
                                       @RequestParam(value = "deptId", defaultValue = "0") Integer deptId) {

        ResponseDTO result = deptDpermitBService.listNewChannelDpermit(deptId, GlobalDict.STATUS_ENABLE);
        return result;
    }

    /**
     * 渠道权限列表
     */
    @RequestMapping("channel/list")
    public String channelList(HttpServletRequest request,
                              @ModelAttribute McDeptDpermitQuery query,
                              Model model) {

        String keyword = request.getParameter("keyword");
        if (NumberUtil.isNumber(keyword)) {
            query.setDataId(Integer.parseInt(keyword));
            query.setDataName("");
        } else {
            query.setDataName(keyword);
        }
        query.setDataType(DpermitConsts.TYPE_COOPERATION);
        query.setDataLevel(DpermitConsts.COOPERATION_LEVEL_COMPANY);
        query.setDeptId(query.getDeptId());

        List<McDeptDpermit> dpermits = deptDpermitService.listPageByQuery(query);

        McDept obj = NumberUtil.isNotNullOrZero(query.getDeptId()) ? deptService.findByKey(query.getDeptId()) : new McDept();
        McOrganize organize = organizeService.findByKey(obj.getOrgId());

        McDeptDpermitQuery twoQuery = new McDeptDpermitQuery();
        twoQuery.setDeptId(query.getDeptId());
        twoQuery.setDataParentIds(dpermits.stream().map(item -> item.getDataId()).collect(Collectors.toList()));
        List<McDeptDpermit> channels = deptDpermitService.listByQuery(twoQuery);

        Map<Integer, DpermitSimpleDO> map = new LinkedHashMap<>();
        for (McDeptDpermit dpermit : dpermits) {
            DpermitSimpleDO item = DpermitUtil.parseToDpermitSimpleDO(dpermit);
            map.put(item.getDataId(), item);
        }
        for (McDeptDpermit dpermit : channels) {
            DpermitSimpleDO item = DpermitUtil.parseToDpermitSimpleDO(dpermit);
            DpermitSimpleDO parent = map.get(item.getParentId());
            if (parent != null) {
                parent.getSubors().add(item);
            }
        }

        model.addAttribute("items", map.values());
        model.addAttribute("vo", query);

        model.addAttribute("obj", obj);
        model.addAttribute("organize", organize);

        model.addAttribute("keyword", keyword);

        // 是否拥有"权限管理员角色"
        model.addAttribute("isAssignAdmin", staffBService.isAssignAdmin(getStaffId()));

        return "dept/dpermit/channel/list";
    }

    /**
     * 部门账户权限树状列表
     */
    @RequestMapping("account/tree")
    public String accountTree(HttpServletRequest request,
                              @RequestParam(value = "deptId", defaultValue = "0") Integer deptId,
                              Model model) {

        McDept obj = NumberUtil.isNotNullOrZero(deptId) ? deptService.findByKey(deptId) : new McDept();
        McOrganize organize = organizeService.findByKey(obj.getOrgId());

        model.addAttribute("obj", obj);
        model.addAttribute("organize", organize);

        // 是否拥有"权限管理员角色"
        model.addAttribute("isAssignAdmin", staffBService.isAssignAdmin(getStaffId()));

        return "dept/dpermit/account/tree";
    }

    @RequestMapping("account/ajaxTree")
    @ResponseBody
    public ResponseDTO accountAjaxTree(HttpServletRequest request, HttpServletResponse response,
                                       @RequestParam(value = "deptId", defaultValue = "0") Integer deptId) {

        ResponseDTO result = deptDpermitBService.listAccountDpermit(deptId, GlobalDict.STATUS_ENABLE);
        return result;
    }


    /**
     * 大数据报表权限树状列表
     */
    @RequestMapping("cube/tree")
    public String cubeTree(HttpServletRequest request,
                           @RequestParam(value = "deptId", defaultValue = "0") Integer deptId,
                           Model model) {

        McDept obj = NumberUtil.isNotNullOrZero(deptId) ? deptService.findByKey(deptId) : new McDept();
        McOrganize organize = organizeService.findByKey(obj.getOrgId());

        model.addAttribute("obj", obj);
        model.addAttribute("organize", organize);

        // 是否拥有"权限管理员角色"
        model.addAttribute("isAssignAdmin", staffBService.isAssignAdmin(getStaffId()));

        return "dept/dpermit/cube/tree";
    }

    @RequestMapping("cube/ajaxTree")
    @ResponseBody
    public ResponseDTO cubeAjaxTree(HttpServletRequest request, HttpServletResponse response,
                                    @RequestParam(value = "deptId", defaultValue = "0") Integer deptId) {

        ResponseDTO<List<MenuDRO>> menusResponse = menuListRemoteService.listAboveByLevel(DpermitConsts.CUBE_LEVEL_FOUR, GlobalDict.STATUS_ENABLE);
        if (!menusResponse.isSuccess()) {
            return ResponseDTO.fail("没有取得菜单列表");
        }
        ResponseDTO result = deptDpermitBService.listCubeDpermit(deptId, GlobalDict.STATUS_ENABLE, menusResponse.getData());
        return result;
    }

    /**
     * 大数据报表指标权限树状列表
     */
    @RequestMapping("measure/tree")
    public String measureTree(HttpServletRequest request,
                              @RequestParam(value = "deptId", defaultValue = "0") Integer deptId,
                              Model model) {

        McDept obj = NumberUtil.isNotNullOrZero(deptId) ? deptService.findByKey(deptId) : new McDept();
        McOrganize organize = organizeService.findByKey(obj.getOrgId());

        model.addAttribute("obj", obj);
        model.addAttribute("organize", organize);

        // 是否拥有"权限管理员角色"
        model.addAttribute("isAssignAdmin", staffBService.isAssignAdmin(getStaffId()));

        return "dept/dpermit/measure/tree";
    }

    @RequestMapping("measure/ajaxTree")
    @ResponseBody
    public ResponseDTO measureAjaxTree(HttpServletRequest request, HttpServletResponse response,
                                       @RequestParam(value = "deptId", defaultValue = "0") Integer deptId) {

        ResponseDTO<List<MeasureDRO>> measureResponse = measureListRemoteService.listMeasureByDataset(GlobalDict.STATUS_ENABLE);
        if (!measureResponse.isSuccess()) {
            return ResponseDTO.fail("没有取得指标列表");
        }
        ResponseDTO result = deptDpermitBService.listMeasureDpermit(deptId, GlobalDict.STATUS_ENABLE, measureResponse.getData());
        return result;
    }


    /**
     * 产品权限树状列表
     */
    @RequestMapping("product/tree")
    public String productTree(HttpServletRequest request,
                              @RequestParam(value = "deptId", defaultValue = "0") Integer deptId,
                              Model model) {

        McDept obj = NumberUtil.isNotNullOrZero(deptId) ? deptService.findByKey(deptId) : new McDept();
        McOrganize organize = organizeService.findByKey(obj.getOrgId());

        model.addAttribute("obj", obj);
        model.addAttribute("organize", organize);

        // 是否拥有"权限管理员角色"
        model.addAttribute("isAssignAdmin", staffBService.isAssignAdmin(getStaffId()));

        return "dept/dpermit/product/tree";
    }

    @RequestMapping("product/ajaxTree")
    @ResponseBody
    public ResponseDTO productAjaxTree(HttpServletRequest request, HttpServletResponse response,
                                       @RequestParam(value = "deptId", defaultValue = "0") Integer deptId) {

        ResponseDTO result = deptDpermitBService.listProductDpermit(deptId, GlobalDict.STATUS_ENABLE);
        return result;
    }
}
